<?php

namespace App\Http\Controllers\Auth;

use App\User;
use Hash;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use Illuminate\Http\Request;
use Gregwar\Captcha\CaptchaBuilder;
use Session;
use Illuminate\Support\Facades\Auth;


class AuthController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Registration & Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users, as well as the
    | authentication of existing users. By default, this controller uses
    | a simple trait to add these behaviors. Why don't you explore it?
    |
    */
    public $username = "username";

    use AuthenticatesAndRegistersUsers, ThrottlesLogins;

    /**
     * Create a new authentication controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest', ['except' => 'getLogout']);
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'username' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|confirmed|min:6',
            'mobile'=>'required|regex:/^1[3458]\d{9}$/'
        ],
        [
            'username.required' => '请输入用户名!',

            'email.required' => '请输入你的邮箱!',
            'email.email' => '请输入正确邮箱!',
            'email.unique' => '此邮箱已被注册!',

            'password.required' => '请输入密码!',
            'password.min' => '密码至少6位',
            'password.confirmed' => '两次输入密码不匹配!',

            'mobile.required'=>'请输入你的手机号!',
            'mobile.regex'=>'请填写正确手机号!'

        ]
        );
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {
        return User::create([
            'username' => $data['username'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
            'mobile' => $data['mobile'],
            'regtime' => time()
        ]);
    }


    /**
     * 登录验证
     */
    public function pLogin(Request $request)
    {
        // echo Session::get('code');
        // dd($request->all());
        //验证用户是否填写用户名和密码
        $validate = Validator::make($request->all(), [

            'username' => 'required', 
            'password' => 'required',
            'imgcode'  => 'required'

        ],
        [
            'username.required' => '请输入用户名!',
            'password.required' =>  '请输入密码!',
            'imgcode.required' =>  '请输入验证码!'
        ]
        );
        if( $validate->fails() ) {
            // 回退到上一页,且携带出错数据,和上次表单中的旧数据
            return back()->withErrors($validate)->withInput();
        }

        //验证码检验
        if (Session::get('code') != strtoupper($request->imgcode)) {
            $validate2 = Validator::make([],[
                        'yanzhengma' => 'required'
                    ],
                    [
                        'yanzhengma.required' => '验证码不正确'
                    ]
                    );

                    if( $validate2->fails() ) {
                    // 回退到上一页,且携带出错数据,和上次表单中的旧数据
                    return back()->withErrors($validate2)->withInput();
                         
                    }
        }

        //用户登录提交过来的数据查数据库,判断是否有该用户存在,密码是否正确
        // $user = new User;
        // $data = $user->where('username','=',$request->username)->first();
        // dd($data->password);
         if (Auth::attempt(['username' => $request->username, 'password' => $request->password],$request->remember)) {
                // 认证通过...
                 return redirect()->intended('/');
                
         }  else {
                //没通过验证
                $validate2 = Validator::make([],[
                        'password' => 'required'
                    ],
                    [
                        'password.required' => '用户名或者密码不正确!'
                    ]
                    );

                    if( $validate2->fails() ) {
                    // 回退到上一页,且携带出错数据,和上次表单中的旧数据
                    return back()->withErrors($validate2)->withInput();

                    }
         }




    }

    //生成验证码
    public function captcha(){
        $builder = new CaptchaBuilder;
        $builder->build();
        Session::put('code',strtoupper($builder->getPhrase()));
        header('Content-type:image/jpeg');
        $builder->output();
    }

}
